package com.yangguang.huahui.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.Attention;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.mvp.presenter.HomePresenter;
import com.yangguang.huahui.mvp.view.HomeView;
import com.yangguang.huahui.ui.activitys.ChangePinActivity;
import com.yangguang.huahui.ui.activitys.ChatActivity;
import com.yangguang.huahui.ui.activitys.ClientNeedsActivity;
import com.yangguang.huahui.ui.activitys.FindFlowersActivity;
import com.yangguang.huahui.ui.activitys.FindMiaomuActivity;
import com.yangguang.huahui.ui.activitys.LoginActivity;
import com.yangguang.huahui.ui.activitys.NongjiActivity;
import com.yangguang.huahui.ui.activitys.NongziActivity;
import com.yangguang.huahui.ui.activitys.PublishActivity;
import com.yangguang.huahui.ui.activitys.QuotesActivity;
import com.yangguang.huahui.ui.adapters.BGABannerAdapter;
import com.yangguang.huahui.ui.adapters.TypePageAdapter;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.widget.TopScrollView;
import com.yangguang.huahui.widget.WrapContentHeightViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTouch;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by Administrator on 2017/3/2.
 */

public class HomeFragment extends BaseMvpFragment<HomeView, HomePresenter> implements HomeView, TopScrollView.OnScrollListener, View.OnTouchListener {

    public static HomeFragment newInstance(String type){
        HomeFragment fragment = new HomeFragment();
        Bundle arguments = new Bundle();
        arguments.putString("type", type);
        fragment.setArguments(arguments);
        return fragment;
    }


    @BindView(R.id.mBgaBanner)
    BGABanner mBgaBanner;
    @BindView(R.id.mViewPager)
    WrapContentHeightViewPager mViewPager;
    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    List<Attention.ListBean> datas = new ArrayList<>();
    private Attention attention;


    private List<BaseFragment> mFragments;
    private List<String> mTitles = new ArrayList<>();
    private TypePageAdapter mTypeAdapter;

    @BindView(R.id.bsv_home)
    TopScrollView bsv_home;
    @BindView(R.id.mLL_attention)
    LinearLayout mLL_attention;
    @BindView(R.id.tv_pinlei)
    TextView tv_pinlei;
    private int layoutTop;
    @BindView(R.id.mLL_top)
    LinearLayout mLL_top;

    @OnClick({R.id.mLL_miaomu, R.id.tv_miaomu, R.id.mLL_flowers, R.id.tv_flowers, R.id.mLL_nongji, R.id.tv_nongji,
            R.id.mLL_nongzi, R.id.tv_nongzi, R.id.mLL_client_needs, R.id.tv_client_needs, R.id.mLL_publish,
            R.id.tv_publish, R.id.tv_pinlei, R.id.mLL_market, R.id.tv_market})
    void onClick(View v){
        switch (v.getId()){
            case R.id.mLL_miaomu:
                startActivity(new Intent(mActivity, FindMiaomuActivity.class));
                break;
            case R.id.tv_miaomu://找苗木
                startActivity(new Intent(mActivity, FindMiaomuActivity.class));
                break;
            case R.id.mLL_flowers:
                startActivity(new Intent(mActivity, FindFlowersActivity.class));
                break;
            case R.id.tv_flowers://找花卉
                startActivity(new Intent(mActivity, FindFlowersActivity.class));
                break;
            case R.id.mLL_nongji:
                startActivity(new Intent(mActivity, NongjiActivity.class));
                break;
            case R.id.tv_nongji://找农机
                startActivity(new Intent(mActivity, NongjiActivity.class));
                break;
            case R.id.mLL_nongzi:
                startActivity(new Intent(mActivity, NongziActivity.class));
                break;
            case R.id.tv_nongzi://找农资
                startActivity(new Intent(mActivity, NongziActivity.class));
                break;
            case R.id.mLL_client_needs:
                startActivity(new Intent(mActivity, ClientNeedsActivity.class));
                break;
            case R.id.tv_client_needs://客户需求
                startActivity(new Intent(mActivity, ClientNeedsActivity.class));
                break;
            case R.id.mLL_publish:
                if (CommonUtil.isNull(DataInfo.TOKEN)) startActivity(new Intent(mActivity, LoginActivity.class));
                else startActivity(new Intent(mActivity, PublishActivity.class));
                break;
            case R.id.tv_publish://发布产品
                if (CommonUtil.isNull(DataInfo.TOKEN)) startActivity(new Intent(mActivity, LoginActivity.class));
                else startActivity(new Intent(mActivity, PublishActivity.class));
                break;
            case R.id.tv_pinlei:
                if (attention == null) return;
                startActivity(new Intent(mActivity, ChangePinActivity.class).putExtra("attention", attention));
                break;
            case R.id.mLL_market:
                startActivity(new Intent(mActivity, QuotesActivity.class));
                break;
            case R.id.tv_market://行情
                startActivity(new Intent(mActivity, QuotesActivity.class));
                break;
        }
    }
    @Override
    protected int initLayoutId() {
        return R.layout.index_home;
    }

    @Override
    protected void initView() {
        List<String> banners = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        titles.add("");
        titles.add("");
        titles.add("");
        titles.add("");
        banners.add("http://www.cnitlabor.com/Public/Uploads/5875a9ff15fd2.png");
        banners.add("http://www.cnitlabor.com/Public/Uploads/58a43d95074b5.png");
        banners.add("http://www.cnitlabor.com/Public/Uploads/58a65ba792419.png");
        banners.add("http://www.cnitlabor.com/Public/Uploads/586b8ca4ceae1.png");
        mBgaBanner.setAdapter(new BGABannerAdapter(mActivity));
        mBgaBanner.setData(banners, titles);
        bsv_home.setOnScrollListener(this);
        bsv_home.setOnTouchListener(this);
    }

    @Override
    protected void initData() {
        mFragments = new ArrayList<>();
    }


    @Override
    public void onTabs(Attention result) {
        attention = result;
        if (result.getList() != null) {
            datas = result.getList();
        }
        mHandler.sendEmptyMessage(1001);
    }

    @Override
    public void onError() {

    }

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1001){
                mTabLayout.removeAllTabs();
                for (int i = 0; i < datas.size(); i++) {
                    mTabLayout.addTab(mTabLayout.newTab().setText(datas.get(i).getClassname()));
                    mTitles.add(datas.get(i).getClassname());
                    mFragments.add(AttentionFragment.newInstance(datas.get(i).getId()));
                }
                setPagerAdapter(mFragments, mTitles);
            }
        }
    };

    private void setPagerAdapter(List<BaseFragment> mFragments, List<String> mTitles){
        mTypeAdapter = new TypePageAdapter(getChildFragmentManager());
        mTypeAdapter.setData(mFragments, mTitles);
        mViewPager.setAdapter(mTypeAdapter);
        mViewPager.setOffscreenPageLimit(mTitles.size() - 1);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(mTypeAdapter);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected HomePresenter initPresenter() {
        return new HomePresenter();
    }

    @Override
    protected void fetchData() {
        if (mPresenter != null)
            mPresenter.getTabs();
    }

    public void onWindowFocusChanged() {
        if (mLL_attention != null){
            layoutTop = mLL_attention.getTop();//获取顶部位置
        }
    }

    @Override
    public void onScroll(int scrollY) {
        if (mLL_attention != null){
            layoutTop = mLL_attention.getTop();//获取顶部位置
        }
        setTop(scrollY);
    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

    private void setTop(int scrollY){
        if(scrollY >= layoutTop){
            if (mTabLayout.getParent() != mLL_top) {
                mLL_attention.removeView(mTabLayout);
                mLL_attention.removeView(tv_pinlei);
                mLL_top.addView(mTabLayout);
                mLL_top.addView(tv_pinlei);
            }
        }else{
            if (mTabLayout.getParent() != mLL_attention) {
                mLL_top.removeView(mTabLayout);
                mLL_top.removeView(tv_pinlei);
                mLL_attention.addView(mTabLayout);
                mLL_attention.addView(tv_pinlei);
            }
        }
    }
}
